// packageE/appointment/technician/appointment_project/appointment_project.js
var location = require("../../../../mybehaviors/location/location");

var app = getApp();
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    url: "",
    projectList: [],
    title: '已预约项目',
    show_worker: false,

    // more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.name != 'sotre') {
      this._getLocation((res, e) => {
        this.setData({
          point: res.location,
          city: res.address_component.city
        })
        this.init();
        this.getData();
      });
    } else {
      this.init();
      this.getData();
    }
  },
  selectTab(e) {
    console.log(e);
    let ind = e.detail.index;
    this.setData({
      active: ind != 2 ? ind : 9,
    });
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      // console.log('没有更多');
      return;
    } else {
      let pages = this.data.page + 1;
      this.getMoreData(pages);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  init() {
    this.page = 1;
    this.isLoadMore = true;
    this.total_page = 0;
    this.projectList = [];
    this.setData({
      page: 1,
      isLoadMore: true,
      total_page: 0,
      projectList: [],
      appointmentLang: wx.getStorageSync("yz_basic_info").lang.appointment,
      show_worker: false
    });
    if (this.options.name === "store") {
      this.setData({
        title: "门店预约",
        url: "plugin.appointment.frontend.appointment-service.get-list-by-store",
        name: 'store'
      });
    } else {
      this.setData({
        title: this.data.appointmentLang.worker + "预约",
        url: "plugin.appointment.frontend.appointment-service.get-list-by-worker",
      });
    }
    wx.setNavigationBarTitle({
      title: this.data.title,
    });
  },
  sure(e) {
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "您确定要取消此预约吗?",
      success: (res) => {
        if (res.confirm) {
          this.cancelDate(id);
        }
      }
    });
  },
  cancelDate(kid) {
    let urlStr = app.getNetAddresss("plugin.appointment.frontend.appointment-service.cancel-service");
    app._getNetWork({
      url: urlStr,
      data: {
        id: kid
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          app.tips(res.msg);
          this.getData();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
  getData() {
    this.init();
    let that = this;
    let urlStr = app.getNetAddresss(this.data.url);
    let json = {
      status: that.data.active,
      page: that.data.page,
    }
    json.lng = this.data.point.lng;
    json.lat = this.data.point.lat;
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            projectList: res.data.data,
            current_page: res.data.current_page,
            last_page: res.data.last_page,
          });
          if (res.data?.set?.show_worker) {
            this.setData({
              show_worker: res.data.set.show_worker ? true : false,
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
  getMoreData(pages) {
    let that = this;
    let urlStr = app.getNetAddresss(this.data.url);
    app._getNetWork({
      url: urlStr,
      data: {
        status: that.data.active,
        page: pages,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            projectList: this.data.projectList.concat(res.data.data),
            current_page: res.data.current_page,
            last_page: res.data.last_page,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
  showWorkPopup(e) {
    let {
      item
    } = e.currentTarget.dataset;
    this.setData({
      show: true,
      keyword: "",
      'popupList.service_id': item.id,
      'popupList.page': 1,
      'popupList.last_page': 0,
    })
    this.getTechnician();
  },
  async getTechnician(e) {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.appointment.frontend.project-goods.get-list-by-store-id"),
      data: {
        page: this.data.popupList.page,
        kwd: this.data.keyword
      },
    });
    if (!data.result) {
      app.tips(data.msg);
      return
    }
    let res = data;
    this.setData({
      'popupList.last_page': res.data.last_page,
      'popupList.data': res.data.data
    })
  },
  async tapMore() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.appointment.frontend.project-goods.get-list-by-store-id"),
      data: {
        page: this.data.popupList.page,
        kwd: this.data.keyword
      },
    });
    if (!data.result) {
      app.tips(data.msg);
      return
    }
    let nextData = res.data.data;
    this.setData({
      'popupList.data': this.data.popupList.data.concat(nextData)
    })
  },
  goToAdress(e){
    console.log(e);
    let {item} = e.currentTarget.dataset;
    let latitude = item.service_address.latitude;
    let longitude = item.service_address.longitude;
    let store_name = item.service_address.address;
    // let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: Number(latitude),
      longitude: Number(longitude),
      scale: 18,
      name: store_name
    });
  },
  async selectTechnician(e) {
    let {
      item
    } = e.currentTarget.dataset;
    console.log(item);
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.appointment.frontend.project-goods.change-worker"),
      data: {
        worker_id: item.id,
        service_id: this.data.popupList.service_id
      },
    });
    console.log(data)
    if (!data.result) {
      app.tips(data.msg)
      return
    }
    wx.showToast({
      title: '更换成功',
      duration:800,
    })
    this.onClickLeft();
    this.getData();
  },
  onClickLeft() {
    this.setData({
      show: false,
      keyword: '',
      'popupList.data': [],
      'popupList.page': 1,
      'popupList.total_page': 0,
    })
  },
});